﻿
using OpenCvSharp;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    [Filter("霍夫变换P", FilterType.Algorithm)]
    public class AlgorithmHoughLinesP:FilterBase
    {
        /// <summary>
        /// 累加器距离
        /// </summary>
        [InputParameter("距离")]
        public double Rho { get; set; }
        /// <summary>
        /// 累加器以弧度为单位的
        /// </summary>
        [InputParameter("角度")]
        public double Theta { get; set; } = Cv2.PI / 180;
        /// <summary>
        /// 累加器阈值参数
        /// </summary>
        [InputParameter("阈值参数")]
        public int Threadhod { get; set; } 
        /// <summary>
        /// 最短长度
        /// </summary>
        [InputParameter("最短长度")] 
        public int MinLineLength { get; set; }
        /// <summary>
        /// 最长长度
        /// </summary>
        [InputParameter("最长长度")] 
        public int MaxLineGap { get; set; }
        /// <summary>
        /// 绘制线段
        /// </summary>
        [InputParameter("绘制线段")] 
        public bool DrawLine { get; set; }
        [InputParameter("颜色")]
        public Scalar LineColor { get; set; }
        /// <summary>
        /// 粗细
        /// </summary>
        [InputParameter("粗细")]
        public int Thickness { get; set; }
        /// <summary>
        /// 线条类型
        /// </summary>
        [InputParameter("线条类型")]
        public LineTypes LineTypes { get; set; }
        /// <summary>
        /// 偏移量
        /// </summary>
        [InputParameter("缩小值")]
        public int Shift { get; set; }
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            var points=  Cv2.HoughLinesP(mat,Rho,Theta,Threadhod,MinLineLength, MaxLineGap);
            if (points!=null&&points.Length>0)
            {
                if (DrawLine)
                {
                    for (int i = 0; i < points.Length; i++)
                    {
                        var l = points[i];
                        Cv2.Line(mat, l.P1, l.P2, LineColor, Thickness, LineTypes, Shift);
                    }
                }

            }
            return ResultData<Mat>.Accept(TextResource.filter_action_complete, mat);
        }
    }
}
